package com.hdoit.pisces.web.user.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;



import com.alibaba.druid.util.StringUtils;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.logical.superbase.model.RoleEmployee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.viewmodel.superbase.EmployeeSearch;
import com.tennetcn.common.logical.viewmodel.superbase.UpdatePwd;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.MD5Utils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.web.superbase.controllers.EmployeeController;

@Controller
@RequestMapping(value="/admin/user/manager/")
public class ManagerController extends EmployeeController{
	
	@Override
	public String getRoutePath(){
		return "areas/admin/user/manager/";
	}
	
	private IRoleEmployeeService roleEmployeeService;
	
	public IRoleEmployeeService getRoleEmployeeService(){
		return roleEmployeeService;
	}
	
	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService){
		this.roleEmployeeService = roleEmployeeService;
	}
	
	private IEmployeeService employeeService;
	
	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String employeeDetailedViewName = "employeeDetailed";
	private final String saveViewName = "save";
	private final String checkCodeViewName = "checkCode";
	private final String checkAccountViewName = "checkAccount";
	private final String personalInformationViewName = "personalInformation";
	private final String updatePwdViewName = "updatePwd";
	
	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model,PagerModel pagerModel,EmployeeSearch search,boolean ajaxPost) {
//		pagerModel.setTotalAmount((employeeService.queryCountMP(search)));
	
//		for(int i = employees.size()-1; i >=0;i--){
//			if(!employees.get(i).getEmployeeId().equals(key)){
//				employees.remove(i);
//			}
//				
//		}
		//pagerModel.setTotalAmount((employeeService.queryCount(search)));
		List<Employee>employeesChoose=employeeService.queryListByRoleMark("3");
		List<Employee> employees = employeeService.queryList(search);
//		for(int i=0;i<employees.size();i++){
//			if(employees.get(i).getRoleIds()==)
//		}
		System.out.println(employees.size()+"99999999999999999999999999999999");
		System.out.println(employeesChoose.size()+"zzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
		int key=0;
		for(int i=employees.size()-1;i>=0;i--){
			
			for(int j=employeesChoose.size()-1;j>=0;j--){
				if(!employees.get(i).getEmployeeId().equals(employeesChoose.get(j).getEmployeeId())){
					continue;
				}else{
					key++;
					break;
				}	
			}
			if(key==0){
				employees.remove(i);
				
			}else{
				key=0;
			}
		}
		
		pagerModel.setTotalAmount(employees.size());
		
		//List<RoleEmployee>role_employees=roleEmployeeService.queryList(search);
		
		//Employee employee = new Employee();
		
		//model.addAttribute("role_employees", role_employees);
		model.addAttribute("employees", employees);
		model.addAttribute("employeeSearch",search);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		
		if(ajaxPost){
		//	System.out.printf(getSessionWebFactory().getCurrentLogin().getLoginName());
			//System.out.printf(employee.getEmployeeName());
			return view(outputViewName);
		}
		//System.out.printf(getSessionWebFactory().getCurrentLogin().getLoginName());
		//System.out.printf(employee.getEmployeeName());
		return view(indexViewName);
	}

	
	@RequestMapping(value = employeeDetailedViewName, method = { RequestMethod.GET })
	public String employeeDetailed(Model model,String employeeId) {
		Employee employee=new Employee();
		employee.setSex(SexStatus.male);
		if(!StringUtils.isEmpty(employeeId)){
			employee=employeeService.queryModel(employeeId);
		}
		
		model.addAttribute("employee", employee);
		
		return getRoutePath() + employeeDetailedViewName;
	}
	
	
	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model,Employee employee) {
		 if(StringUtils.isEmpty(employee.getEmployeeId())){
			 //新增
			 employee.setEmployeeId(CommonUtils.getKeyId());
			 employee.setCreateDate(DateUtils.getCurrent());
			 employee.setCreateUserId(sessionWebFactory.getCurrentLogin().getLoginId());
			 employee.setCreateUserName(sessionWebFactory.getCurrentLogin().getLoginName());
			 employee.setDeleteMark(Integer.parseInt(YesOrNo.no));
			 employee.setIsLocked(YesOrNoInteger.no);
			 employee.setIsLogin(YesOrNoInteger.yes);
			 employee.setModelStatus(ModelStatus.add);
		 }else{
			 employee.setModifyDate(DateUtils.getCurrent());
			 employee.setModifyUserId(sessionWebFactory.getCurrentLogin().getLoginId());
			 employee.setModifyUserName(sessionWebFactory.getCurrentLogin().getLoginName());
			 employee.setModelStatus(ModelStatus.update);
		 }
		 //markcode
		 employee.setMarkCode(ChineseUtils.getPinYinHeadChar(employee.getEmployeeName()));
		 
		
		ActionResult actionResult;
		if(employeeService.applyChange(employee)){
			actionResult=new ActionResult();
		}else{
			actionResult=new ErrorResult();
		}
		model.addAttribute("employee", employee);
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		
		return view(employeeDetailedViewName);
	}
	
	
	@RequestMapping(value = checkCodeViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkCode(String employeeCode,String employeeId){
		return String.valueOf(!employeeService.checkCode(employeeId, employeeCode));
	}
	
	
	@RequestMapping(value = checkAccountViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkAccount(String employeeAccount,String employeeId){
		return String.valueOf(!employeeService.checkAccount(employeeId, employeeAccount));
	}
	
	@RequestMapping(value = personalInformationViewName, method = { RequestMethod.GET })
	public String personalInformation(String employeeCode,String employeeId){
		return view(personalInformationViewName);
	}
	
	@RequestMapping(value = updatePwdViewName, method = { RequestMethod.GET })
	public String updatePwd(Model model){
		UpdatePwd pwd=new UpdatePwd();
		model.addAttribute("updatePwd", pwd);
		
		return view(updatePwdViewName);
	}
	@RequestMapping(value = updatePwdViewName, method = { RequestMethod.POST })
	public String updatePwd(Model model,UpdatePwd updatePwd){
		updatePwd.setEmployeeId(getLoginModel().getLoginId());
		updatePwd.setOldPwd(MD5Utils.encoderByMd5With32Bit(updatePwd.getOldPwd()));
		
		Employee employee=employeeService.loginById(updatePwd.getEmployeeId(), updatePwd.getOldPwd());
		ActionResult actionResult;
		if(employee==null){
			actionResult=new ErrorResult();
			actionResult.setMessage("密码错误");
		}else{
			if(employeeService.updatePwd(updatePwd)){
				actionResult=new ActionResult();
				actionResult.setRefresh(false);
				sessionWebFactory.clearCurrentLogin();
			}else{
				actionResult=new ErrorResult();
			}
		}
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		
		return view(updatePwdViewName);
	}
	
	

}

